﻿using B.S.BaseData.Infratructure.Interfares;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.S.BaseData.Infratructure.Implements
{
    /// <summary>
    /// 实现接口
    /// </summary>
    /// <typeparam name="T">接口</typeparam>
    public class BaseRequsitory<T> : IBaseRequsitory<T> where T : class, new()
    {
        /// <summary>
        /// 数据库上下文
        /// </summary>
        private readonly SCDbCotext db;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="db">db</param>
        public BaseRequsitory(SCDbCotext db)
        {
            this.db = db;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="item">实体</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> AddAsync(T item)
        {
            db.Set<T>().AddAsync(item);
            return db.SaveChangesAsync();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Del(T t)
        {
            db.Set<T>().Remove(t);
            return db.SaveChanges();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">实体</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> DeleteAsync(int id)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns>集合</returns>
        public IQueryable<T> GetAllAsync()
        {
            return db.Set<T>().AsQueryable();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="item">实体</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> UpdateAsync(T item)
        {
            db.Set<T>().Update(item);
            return db.SaveChangesAsync();
        }
        public async Task<int> GetChildrenCountAsync(int parentId)
        {
            return await db.Menus.CountAsync(m => m.PId == parentId);
        }

    }
}
